/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author T.Ron
 */
public class BudgetpbJpaController implements Serializable {

    public BudgetpbJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Budgetpb budgetpb) throws PreexistingEntityException, Exception {
        if (budgetpb.getBudgetpbPK() == null) {
            budgetpb.setBudgetpbPK(new BudgetpbPK());
        }
        budgetpb.getBudgetpbPK().setMois(budgetpb.getPeriode().getPeriodePK().getMois());
        budgetpb.getBudgetpbPK().setNumexo(budgetpb.getPeriode().getPeriodePK().getNumexo());
        budgetpb.getBudgetpbPK().setIdPb(budgetpb.getPostebudgetaire().getIdPb());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Postebudgetaire postebudgetaire = budgetpb.getPostebudgetaire();
            if (postebudgetaire != null) {
                postebudgetaire = em.getReference(postebudgetaire.getClass(), postebudgetaire.getIdPb());
                budgetpb.setPostebudgetaire(postebudgetaire);
            }
            Periode periode = budgetpb.getPeriode();
            if (periode != null) {
                periode = em.getReference(periode.getClass(), periode.getPeriodePK());
                budgetpb.setPeriode(periode);
            }
            em.persist(budgetpb);
            if (postebudgetaire != null) {
                postebudgetaire.getBudgetpbCollection().add(budgetpb);
                postebudgetaire = em.merge(postebudgetaire);
            }
            if (periode != null) {
                periode.getBudgetpbCollection().add(budgetpb);
                periode = em.merge(periode);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findBudgetpb(budgetpb.getBudgetpbPK()) != null) {
                throw new PreexistingEntityException("Budgetpb " + budgetpb + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Budgetpb budgetpb) throws NonexistentEntityException, Exception {
        budgetpb.getBudgetpbPK().setMois(budgetpb.getPeriode().getPeriodePK().getMois());
        budgetpb.getBudgetpbPK().setNumexo(budgetpb.getPeriode().getPeriodePK().getNumexo());
        budgetpb.getBudgetpbPK().setIdPb(budgetpb.getPostebudgetaire().getIdPb());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Budgetpb persistentBudgetpb = em.find(Budgetpb.class, budgetpb.getBudgetpbPK());
            Postebudgetaire postebudgetaireOld = persistentBudgetpb.getPostebudgetaire();
            Postebudgetaire postebudgetaireNew = budgetpb.getPostebudgetaire();
            Periode periodeOld = persistentBudgetpb.getPeriode();
            Periode periodeNew = budgetpb.getPeriode();
            if (postebudgetaireNew != null) {
                postebudgetaireNew = em.getReference(postebudgetaireNew.getClass(), postebudgetaireNew.getIdPb());
                budgetpb.setPostebudgetaire(postebudgetaireNew);
            }
            if (periodeNew != null) {
                periodeNew = em.getReference(periodeNew.getClass(), periodeNew.getPeriodePK());
                budgetpb.setPeriode(periodeNew);
            }
            budgetpb = em.merge(budgetpb);
            if (postebudgetaireOld != null && !postebudgetaireOld.equals(postebudgetaireNew)) {
                postebudgetaireOld.getBudgetpbCollection().remove(budgetpb);
                postebudgetaireOld = em.merge(postebudgetaireOld);
            }
            if (postebudgetaireNew != null && !postebudgetaireNew.equals(postebudgetaireOld)) {
                postebudgetaireNew.getBudgetpbCollection().add(budgetpb);
                postebudgetaireNew = em.merge(postebudgetaireNew);
            }
            if (periodeOld != null && !periodeOld.equals(periodeNew)) {
                periodeOld.getBudgetpbCollection().remove(budgetpb);
                periodeOld = em.merge(periodeOld);
            }
            if (periodeNew != null && !periodeNew.equals(periodeOld)) {
                periodeNew.getBudgetpbCollection().add(budgetpb);
                periodeNew = em.merge(periodeNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                BudgetpbPK id = budgetpb.getBudgetpbPK();
                if (findBudgetpb(id) == null) {
                    throw new NonexistentEntityException("The budgetpb with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(BudgetpbPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Budgetpb budgetpb;
            try {
                budgetpb = em.getReference(Budgetpb.class, id);
                budgetpb.getBudgetpbPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The budgetpb with id " + id + " no longer exists.", enfe);
            }
            Postebudgetaire postebudgetaire = budgetpb.getPostebudgetaire();
            if (postebudgetaire != null) {
                postebudgetaire.getBudgetpbCollection().remove(budgetpb);
                postebudgetaire = em.merge(postebudgetaire);
            }
            Periode periode = budgetpb.getPeriode();
            if (periode != null) {
                periode.getBudgetpbCollection().remove(budgetpb);
                periode = em.merge(periode);
            }
            em.remove(budgetpb);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Budgetpb> findBudgetpbEntities() {
        return findBudgetpbEntities(true, -1, -1);
    }

    public List<Budgetpb> findBudgetpbEntities(int maxResults, int firstResult) {
        return findBudgetpbEntities(false, maxResults, firstResult);
    }

    private List<Budgetpb> findBudgetpbEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Budgetpb.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Budgetpb findBudgetpb(BudgetpbPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Budgetpb.class, id);
        } finally {
            em.close();
        }
    }

    public int getBudgetpbCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Budgetpb> rt = cq.from(Budgetpb.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
